<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
    var tile; 

    var map, layer;
    function setUp() {
        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer(null, {
            isBaseLayer: true
        });
        map.addLayer(layer)
        map.setCenter(new OpenLayers.LonLat(0, 0));
    }
    
    function tearDown() {
        map.destroy();
        map = null;
        layer = null;
    }

    
    function test_Tile_constructor (t) {
        t.plan( 13 );
     
        setUp();
        
        var dummy = {};

        var position = new OpenLayers.Pixel(10,20);
        var bounds = new OpenLayers.Bounds(1,2,3,4);
        var url = "bobob";
        var size = new OpenLayers.Size(5,6);
        
        tile = new OpenLayers.Tile(layer, position, bounds, url, size, {
            eventListeners: {
                loadstart: OpenLayers.Function.False
            }
        });

        t.ok(tile instanceof OpenLayers.Tile, "new OpenLayers.Tile returns Tile object");
        t.ok(tile.layer === layer, "tile.layer set correctly");
        t.ok(tile.position.equals(position), "tile.position set correctly");
        t.ok(tile.position != position, "tile.position set not by reference");
        t.ok(tile.bounds.equals(bounds), "tile.bounds set correctly");
        t.ok(tile.bounds != bounds, "tile.bounds set not by reference");
        t.eq(tile.url, url, "tile.url set correctly");        
        t.ok(tile.size.equals(size), "tile.size is set correctly");
        t.ok(tile.size != size, "tile.size set not by reference");

        t.ok(tile.id != null, "tile is given an id");
        t.ok(OpenLayers.String.startsWith(tile.id, "Tile_"),
             "tile's id starts correctly");
        t.ok(tile.events != null, "tile's events initialized");
        t.ok(tile.events.listeners.loadstart.length == 1, 
             "tile's events initialized from eventListeners option");
        
        tearDown();
        
    }
    
    function test_Tile_draw(t) {
        t.plan(6);
        setUp();

        var position = new OpenLayers.Pixel(10,20);
        var bounds = new OpenLayers.Bounds(1,2,3,4);
        var url = "bobob";
        var size = new OpenLayers.Size(5,6);
        
        tile = new OpenLayers.Tile(layer, position, bounds, url, size);
        var log = [];
        tile.clear = function() {
            log.push("clear");
        }
        tile.draw();
        t.eq(log.length, 1, "Tile cleared before drawing");
        
        log = [];
        tile.events.register("beforedraw", this, function() {
            log.push("beforedraw");
            return false;
        });
        var drawn = tile.draw();
        t.eq(log[0], "clear", "tile cleared");
        t.eq(log[1], "beforedraw", "beforedraw event fired");
        t.eq(drawn, null, "tile not drawn when beforedraw listener returns false");
        drawn = tile.draw(true);
        t.eq(log.length, 2, "no beforedraw event fired and tile not cleared when draw called with 'deferred' argument set to true");
        t.eq(drawn, true, "tile drawn when draw called with 'deferred' argument set to true");

        tearDown();
    }

    function test_Tile_destroy(t) {
        t.plan( 6 );
        
        setUp();

        var position = new OpenLayers.Pixel(10,20);
        var bounds = new OpenLayers.Bounds(1,2,3,4);
        var url = "bobob";
        var size = new OpenLayers.Size(5,6);
        
        tile = new OpenLayers.Tile(layer, position, bounds, url, size);
        tile.events.destroy = function() {
            t.ok(true, "tile events destroy() called");
        };

 
        tile.destroy();

        t.ok(tile.layer == null, "tile.layer set to null");
        t.ok(tile.bounds == null, "tile.bounds set to null");
        t.ok(tile.size == null, "tile.size set to null");
        t.ok(tile.position == null, "tile.position set to null");
        
        t.ok(tile.events == null, "tile.events set to null");
        
        tearDown();
        
    }

  </script>
</head>
<body>
</body>
</html>

